import "dart:convert";

import "package:ct_pa_majordomo/config/theme_vars.dart";
import "package:ct_pa_majordomo/pages/device/lock/api.dart";
import "package:ct_pa_majordomo/pages/device/lock/setting/widgets/pwd_manage/index.dart";
import "package:ct_pa_majordomo/pages/widgets/menu_block.dart";
import "package:ct_pa_majordomo/router/keys.dart";
import "package:ct_pa_majordomo/utils/stacker.dart";
import "package:ct_pa_majordomo/widgets/divider/solid_divider.dart";
import "package:flutter/cupertino.dart";
import "package:flutter/material.dart";
import "package:ct_pa_majordomo/pages/device/lock/setting/widgets/electricity/index.dart";
import "package:flutter_easyloading/flutter_easyloading.dart";

class Base extends StatefulWidget {
  final Map? info;
  const Base({super.key, required this.info});

  @override
  State<StatefulWidget> createState() => _BaseState();

}

class _BaseState extends State<Base> {

  Map _params = {};
  late Map _lockInfo;

  @override
  void didChangeDependencies() {
    setState(() {
      _params = widget.info!;
    });
    super.didChangeDependencies();
  }

  void _editLock() async {
    Map<String, dynamic> params = {
      "lockId": _params["lockId"],
      "lockName": _params["lockName"],
    };
    await lockApi.editLock(params);
    EasyLoading.showToast("修改成功");
  }

  // 编辑锁名
  void _editLockName({
    required BuildContext context,
    required String value,
    ValueChanged<String>? onChange,
  }) async {
    String textValue = value;
    showDialog(
        context: context,
        builder: (BuildContext context) {
          return Dialog(
            shape: const RoundedRectangleBorder(
              borderRadius: BorderRadius.all(Radius.circular(10)),
            ),
            child: ListView(
              shrinkWrap: true,
              children: [
                const Row(
                  mainAxisAlignment: MainAxisAlignment.center,
                  children: [
                    Padding(
                      padding: EdgeInsets.symmetric(vertical: 10),
                      child: Text("锁名", style: TextStyle(fontSize: 18),),
                    ),
                  ],
                ),
                Padding(
                  padding: const EdgeInsets.symmetric(horizontal: 10),
                  child: TextFormField(
                    autofocus: true,
                    decoration: const InputDecoration(
                      hintText: "请输入",
                      border: InputBorder.none,
                    ),
                    initialValue: textValue,
                    onChanged: (val) {
                      textValue = val;
                    },
                  ),
                ),
                const SolidDivider(),
                SizedBox(
                  height: 50,
                  child: Row(
                    children: [
                      Expanded(
                        child: TextButton(
                          onPressed: () {
                            Navigator.of(context).pop();
                          },
                          child: const Text("取消", style: TextStyle(color: ThemeVars.colorTextTer),),
                        ),
                      ),
                      const SolidDivider(direction: Axis.vertical,),
                      Expanded(
                        child: TextButton(
                          onPressed: () {
                            if (textValue.isEmpty) return;
                            if (onChange != null) onChange(textValue);
                            Navigator.of(context).pop();
                          },
                          child: const Text("确定")
                        ),
                      ),
                    ],
                  ),
                )
              ],
            ),
          );
        }
    );
  }

  @override
  void initState() {
    setState(() {
      _lockInfo = jsonDecode(widget.info?["lockInfo"] ?? "");
    });
    super.initState();
  }


  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        iconTheme: const IconThemeData(color: Colors.white),
        title: const Text("基本信息", style: TextStyle(color: Colors.white),),
        centerTitle: true,
        backgroundColor: ThemeVars.colorPrimaryDark,
        leading: IconButton(
          icon: const Icon(Icons.arrow_back_ios_new),
          onPressed: () {
            Navigator.pop(context);
          },
        ),
      ),
      body: ListView(
        children: [
          MenuBlock(
              borderRadius: const BorderRadius.all(Radius.circular(0)),
              items: [
                MenuBlockItem(
                  text: "编号",
                  showArrow: false,
                  suffix: Text(_lockInfo?["lockName"], style: TextStyle(color: ThemeVars.colorTextTer),),
                ),
                MenuBlockItem(
                  text: "MAC/ID",
                  showArrow: false,
                  suffix: Text("${_lockInfo?["lockMac"]}/${_lockInfo?["lockId"]}", style: TextStyle(color: ThemeVars.colorTextTer),),
                ),
                MenuBlockItem(
                  text: "电量",
                  suffix: Text("${_lockInfo["electricQuantity"]}%", style: TextStyle(color: ThemeVars.colorTextTer),),
                  onTap: () {
                    Stacker.pushNamed(Keys.DEVICE_LOCK_SETTING_POWER, arguments: widget.info);
                    // Navigator.push(
                    //   context,
                    //   CupertinoPageRoute(
                    //     builder: (BuildContext context) {
                    //       return const Electricity();
                    //     },
                    //   ),
                    // );
                  }
                ),
                MenuBlockItem(
                  text: "名称",
                  suffix: Text(_params["lockName"], style: const TextStyle(color: ThemeVars.colorTextTer),),
                  onTap: () {
                    _editLockName(
                      context: context,
                      value: _params["lockName"],
                      onChange: (value) => setState(() {
                        _params["lockName"] = value;
                        _editLock();
                      }),
                    );
                  }
                ),
                MenuBlockItem(
                  text: "锁分组",
                  showArrow: false,
                  suffix: const Text("分组一", style: TextStyle(color: ThemeVars.colorTextTer),),
                ),
                MenuBlockItem(
                  text: "管理员开锁密码",
                  onTap: () {
                    Stacker.pushNamed(Keys.DEVICE_LOCK_SETTING_ADMIN_PASSWORD, arguments: widget.info);
                  }
                ),
              ]
          ),
        ],
      )
    );
  }

}